<template>
  <div>
      <div class="header">
          <span class="iconfont iconjiantou2" @click="back"></span>
          <div class="middle">
              <span class="iconfont iconsearch"></span>
              <!-- 输入框双向绑定一个数据 -->
              <input type="text" class="searchtext" placeholder="请搜索" v-model="searchText">
          </div>
          <div class="btn" @click="search">搜索</div>
      </div>
      <div v-if="resultList.length>0">
          <post :post="item" v-for="(item,index) in resultList" :key="index"></post>
      </div>
      <div class="suggestion" v-else>
          <div class="history">
              <h2>历史记录</h2>
              <span v-for="(item,index) in historyList" :key="index"
              @click="searchByWord(item)">{{item}}</span>
          </div>
          <div class="hot">
              <h2>热门搜索</h2>
              <span v-for="(item,index) in hotList" :key="index" @click="searchByWord(item)">{{item}}</span>
          </div>
      </div>
  </div>
</template>

<script>
import post from '../components/post'
export default {
    components:{
        post
    },
    data(){
        return{
            searchText:'',
            resultList:[],
            // 下面这个数组用来存放假数据
            hotList:[],
            // 下面这个数组用来记录历史记录
            historyList:localStorage.getItem('searchHistory')?JSON.parse(localStorage.getItem('searchHistory')):[]
        }
    },
    // 每次搜索词的变化，我都监听
    watch:{
        // 搜索词清空时，数据也自然显示不出来
        searchText(){
            if(this.searchText == ""){
                this.resultList = [];
            }
        },
        // 拿到数据后，将数据放入本地存储
        historyList(){
            // 将数据先转换成字符串后再存储
            const strHistory = JSON.stringify(this.historyList);
            localStorage.setItem('searchHistory',strHistory);
        }
    },
    mounted(){
        this.getHotList();
    },
    methods:{
        back(){
            if(this.searchText){
                this.searchText = ''
            }else{
                this.$router.back();
            }
        },
        searchByWord(item){
            this.searchText = item;
            this.search();
        },
        getHotList(){
            // 应该发送ajax请求，获取到热门搜索数组
            this.hotList = [
                '关晓彤',
                '王祖贤',
                '女歌手'
            ]
        },
        search(){
            // 获取输入框里面的关键词，发送请求
            this.$axios({
                url:'/post_search',
                method:'get',
                params:{
                    keyword:this.searchText
                }
            }).then(res=>{
                const {data} = res.data;
                console.log(data);
                this.resultList = data;
                // 搜索成功后，应该将搜索记录存放在historyList中
                // 先判断这个词是否已经存在于数组中
                if(this.historyList.indexOf(this.searchText)<0){
                    this.historyList.push(this.searchText)
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>
.header{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 2.778vw;
}
.middle{
    width: 65.667vw;
    height: 10.556vw;
    line-height: 10.556vw;
    border: 1px solid #888;
    border-radius: 5.278vw;
    padding: 0 4.167vw;
    .searchtext{
        height: 9vw;
        line-height: 9vw;
        width: 55.556vw;
        border: none;
        outline: none;
    }
}
</style>